package lc.字符串;

/**
 * @description: 压缩字符串
 * @author: qian
 * @createDate: 2021/8/21
 */
public class Math443 {

    //lc.双指针
    public int compress(char[] chars) {

        int len = chars.length;
        StringBuffer sb = new StringBuffer();
        int left = 0;
        int right = 0;
        while(left < len) {
            while(right < len && chars[left] == chars[right]) {
                right++;
            }
            sb.append(chars[left]);
            if(right - left > 1) {
                sb.append(right - left);
            }
            left = right;
        }
        for(int i = 0; i < sb.length(); i++) {
            chars[i] = sb.charAt(i);
        }
        return sb.length();
    }

}
